﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.DALFactory;
using AspNetCs.Model;
using AspNetCs.Common;
using System.Data;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Config;
using System.Web;
using System.Reflection;

namespace AspNetCs.Bll
{
    public class Cemetery
    {
        #region ICemetery 成员
        ICemetery CreateCemetery = Conn.CreateCemetery();

        public int Add(AspNetCs.Model.Cemetery_Model md, out string Msg)
        {
            Msg = "参数不能为空";
            if (md == null) return -2;
            int result = CreateCemetery.Add(md);
            if (result > 0)
            {
                Msg = "添加成功!";
            }
            else if (result == -1)
            {
                Msg = "添加失败!";
            }
            return result;
        }

        public AspNetCs.Model.Cemetery_Model GetModel(System.Data.DataRow dr)
        {
            Cemetery_Model model = new Cemetery_Model();
            try
            {
                model = CreateCemetery.GetModel(dr);
            }
            catch
            {
                model = null;
            }
            finally
            {

            }


            return model;
        }

        public AspNetCs.Model.Cemetery_Model GetModel(int id)
        {

            DataTable dt = GetDataTable(" id=" + id);

            Cemetery_Model md = new Cemetery_Model();
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    md = CreateCemetery.GetModel(dr);
                }
            }
            return md;
        }

        public System.Data.DataTable GetDataTable(string filter)
        {
            if (string.IsNullOrEmpty(filter)) { return null; }
            return CreateCemetery.GetDataTable(filter);
        }

        public int delete(string filter)
        {
            return CreateCemetery.delete(filter);
        }


        /// <summary>
        /// 根据当前馆号,生成用模板
        /// </summary>
        /// <param name="NumStr">馆号</param>
        /// <param name="chel"></param>
        /// <param name="systemConfig">系统配制信息</param>
        /// <param name="htmlpath">生成的路径</param>
        /// <returns></returns>
        public bool CurrTempleContent(string NumStr, Channel_Type chel, SiteConfigInfo systemConfig, string htmlpath)
        {
            bool Result = false;
            StringBuilder stb = new StringBuilder();
            Cemetery_Model md = new Cemetery_Model();
            try
            {

                DataTable dt = GetDataTable(" Numid='" + NumStr + "'");
                dt.TableName = "Cemetery";
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        md = GetModel(dr);
                        break;
                    }
                    if (md.TempleId > 0)
                    {
                        Temple_Model TmpMd = TempleBll.CreateNew.GetModel(md.TempleId);
                        if (TmpMd.Id > 0)
                        {
                            string TempleUrl = systemConfig.InstallDir + StringHelper.ReadConfig("TempleSkin") + chel.ToString() + "/" + TmpMd.TemplePath + "/index.html";
                            string Templepath = HttpContext.Current.Server.MapPath(TempleUrl);//模板的真实路径

                            StringBuilder newstr = new StringBuilder();
                            newstr.AppendLine("<script>");
                            newstr.AppendLine("<!--");
                            newstr.AppendLine("var Numid='" + md.Numid + "';");//陵园馆号
                            newstr.AppendLine("var id='" + md.Id + "';");//陵园ID
                            newstr.AppendLine("var UserID='" + md.CreateId + "';");//陵园名称
                            newstr.AppendLine("var Pid='" + md.Province + "';");//省份
                            newstr.AppendLine("var CityID='" + md.City + "';");//城市
                            newstr.AppendLine("-->");
                            newstr.AppendLine("</script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/jquery.js'></script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/Cemetery.Aspx'></script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/Cememtery_AddMsg.aspx'></script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/Cemetery_load.aspx?id=" + md.Id + "&rnd=" + new Random().Next(1000, 9999999) + "'></script>");

                            stb = TempleBll.CreateNew.WriteHtml(Templepath, newstr.ToString());//读取模板内容

                            GetStringData_lable lbReplace = new GetStringData_lable();

                            #region 循环所有属性
                            //newstr.AppendLine("<script>");
                            //newstr.AppendLine("<!--");

                            //PropertyInfo[] props = typeof(Cemetery_Model).GetProperties();
                            //foreach (PropertyInfo p in props)
                            //{
                            //    newstr.AppendLine("var " + p.Name.ToString() + "='" + p.GetValue(md, null) + "';");
                            //}
                            //newstr.AppendLine("-->");
                            //newstr.AppendLine("</script>");


                            #endregion

                            string ContentValue = lbReplace.GetResultStr(stb, dt);
                            Result = TempleBll.CreateNew.CreateHtmlFile(ContentValue, htmlpath);
                        }
                    }
                }
            }
            catch
            {
                Result = false;
            }

            return Result;
        }

        /// <summary>
        /// 设置推荐或者取消推荐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示设置推荐 0表示取消推荐</param>
        /// <returns></returns>
        public int SetIntro(int Id, int CancleOrIntro)
        {
            return CreateCemetery.SetIntro(Id, CancleOrIntro);
        }


        /// <summary>
        /// 审核或者取消审核
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示审核 0表示取消审核</param>
        /// <returns></returns>
        public int SetCheck(int Id, int CancleOrIntro)
        {
            return CreateCemetery.SetCheck(Id, CancleOrIntro);
        }
        /// <summary>
        /// 更新字段
        /// </summary>
        /// <param name="Upfilter">更新字段</param>
        /// <param name="filter">条件</param>
        public void filterUpdate(string Upfilter, string filter)
        {
            if (Upfilter != "")
            {
                CreateCemetery.filterUpdate(Upfilter, filter);
            }
        }

        /// <summary>
        /// 设置背景音乐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="MusicId"></param>
        /// <returns></returns>
        public int SetMusic(int Id, int MusicId)
        {
            return CreateCemetery.SetMusic(Id, MusicId);

        }

        #endregion
    }
}
